package org.feng.entity;

import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * 电脑实体，在初始化时，需要设置一些属性值
 *
 * @version V1.0
 * @author: fengjinsong
 * @date: 2022年06月29日 10时59分
 */
@PostProcessor
@Slf4j
@Component
@ToString
public class Computer implements InitializingBean, BeanNameAware, DisposableBean {

    /**
     * 主板
     */
    @Resource
    private Motherboard motherboard;

    @Resource
    private Cpu cpu;

    @Autowired
    private Cpu oldCpu;

    @Resource(name = "oldCpu")
    private Cpu cpu1;

    @Autowired
    @Qualifier(value = "cpuWithBeanName")
    private Cpu cpu2;

    private String beanName;

    @Value("${computer.server:北京某地}")
    private String server;
    @Value("${computer.server1}")
    private String server1;

    public Computer() {
        log.info("Computer 构造器正在执行");
    }

    @Override
    public void afterPropertiesSet() {
        log.info("afterPropertiesSet() start");
        log.info("Computer {}", this);
        log.info("afterPropertiesSet() end");
    }

    @PostConstruct
    private void init() {
        log.info("PostConstruct#init() start");
        log.info("Computer {}", this);
        log.info("PostConstruct#init() end");
    }

    @PreDestroy
    private void destroyByAnnotation() {
        log.info("PreDestroy#destroy() start");
        log.info("Computer {}", this);
        log.info("PreDestroy#destroy() end");
    }

    @Override
    public void destroy() {
        log.info("DisposableBean#destroy() start");
        log.info("Computer {}", this);
        log.info("DisposableBean#destroy() end");
    }

    @Override
    public void setBeanName(String beanName) {
        this.beanName = beanName;
        log.info("使用BeanNameAware 获取到 beanName {}", beanName);
    }
}
